﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bigger.Study.ConsoleApp.PostSharp
{
    //[MethodAspect3(SelfPropertyName = "在类的自定义属性")]
    public class PostSharpExample : IExample
    {
        public void Execute()
        {
            var result = this.TestMethod6().ToList();
            //foreach (int i in result)
            //{

            //}
        }

        [MethodAspect1]
        public string TestMethod1(string s1, string s2)
        {
            Console.WriteLine("PostSharpExample.TestMethod1(s1,s2)");
            return "TestResult";
        }

        [MethodAspect1(AspectPriority = 1)]
        [MethodAspect2(AspectPriority = 0)]
        public string TestMethod2(string s1, string s2)
        {
            //AspectPriority是postsharp类中对多个拦截器进行优先级设定的方式，值越小说明执行优先级越高，反之者越低。
            Console.WriteLine("PostSharpExample.TestMethod2(s1,s2)");
            return "TestResult";
        }

        [MethodAspect1]
        public string TestMethod3(string s1, string s2)
        {
            Console.WriteLine("PostSharpExample.TestMethod3(s1,s2)");
            throw new Exception("PostSharpExample.TestMethod3执行过程中发生异常");
            return "TestResult";
        }

        [MethodAspect1]
        [MethodAspect2]
        public string TestMethod4(string s1, string s2)
        {
            Console.WriteLine("PostSharpExample.TestMethod4(s1,s2)");
            try
            {
                throw new Exception("PostSharpExample.TestMethod3执行过程中发生异常");
            }
            catch (Exception exception)
            {
                if (s1 == "222")
                {
                    throw exception;
                }
            }
            return "TestResult";
        }

        [MethodAspect3(SelfPropertyName = "在方法的自定义属性", AttributeReplace = true)]
        public string TestMethod5(string s1, string s2)
        {
            //AttributeReplace会覆盖掉类同名的特性
            Console.WriteLine("PostSharpExample.TestMethod5(s1,s2)");
            return "TestResult";
        }

        [MethodAspect1]
        public IEnumerable<int> TestMethod6()
        {
            //ToList 转到本地内存的时候方法（包括AOP）才执行，
            for (int i = 0; i < 10; i++)
            {
                yield return i;
                //yield后面的代码仍会执行
                //Console.WriteLine("Hello World");
            }

            //Console.WriteLine("Hello World");
        }
    }
}
